//==================================================
// D_NC_scale.ino
// 2015-01-28
// Version 0.1
//==================================================
// Nabton Client Generic Configuration
//==================================================
 #define NABTON_DATA 1

 #define SIDN  3
 #define SID1 901
 #define SID2 902
 #define SID3 903
 
 #define PIN_LED_STATUS 13
//==================================================
// Nabton Client Application Configuration
//==================================================
 
  // Big display
  //int   zero_level  = 1290;
  //float refKg       = 86.7;
  //int   refValue    = 3030; 
  //float noice_level = 0.1;
  // Small display
  int   zero_level  = 725;
  float refKg       = 86.7;
  int   refValue    = 2026; 
  float noice_level = 0.2;
  #define N_FILTER 5
  #define N_LEDS 3

int g_led[10];
int cur;
int g_selected_sid;


int g_sid;
float factor;
float oldTs,newTs;
int value = HIGH;
int nvalue = 0;
int count = 0;
float oldw[10];
float sum,ftemp,weight = 0.0;
int ready = 0;
int sid[10];
 
#define PIN_LED_ARM   3
#define PIN_LED_MSG   4
#define PIN_INTERRUPT 2
#define PIN_SIGNAL   12
#define PIN_LED_SID1  5
#define PIN_LED_SID2  6
#define PIN_LED_SID3  7
#define PIN_BUTTON_1 10
#define PIN_BUTTON_2 11


//=================================================
void sendNDS(int sid, float data)
//=================================================
{
  Serial.print(NABTON_DATA);
  Serial.print(" "); 
  Serial.print(sid);
  Serial.print(" "); 
  Serial.print(data);
  Serial.print(";"); 
  Serial.flush();
}
//=================================================
void setLed(int led)
//=================================================
{
  for (int i=1;i<=N_LEDS;i++)
  {
    digitalWrite(g_led[i],LOW);
  }
  digitalWrite(g_led[led],HIGH);
  g_selected_sid = led;
}
//=================================================
int selectSid()
//=================================================
{
  while(digitalRead(PIN_BUTTON_1) == LOW)
  {
    int x2 = digitalRead(PIN_BUTTON_2);
    int par = x2;
  
    switch(par)
    {
     case 0 :
       break;
     case 1 :
       cur++;
       if(cur > N_LEDS)cur=1;
       setLed(cur);
       break;
     default :
      Serial.println("This will not happen\n" );
    }
    delay(150); 
  }
}
//=================================================
void stamp()
//=================================================
{
  int i;
  float gtemp;
  value = digitalRead(PIN_SIGNAL);
  if(value == LOW)
  {
    count++;
  }
  if(count > 0 && value == HIGH)
  {
     nvalue++;
     for(i=N_FILTER;i>0;i--)oldw[i] = oldw[i-1];
     oldw[0]=(count-zero_level)*factor;
     ftemp = 0.0;
     sum = 0.0;
     for(i=0;i<N_FILTER;i++)
     {
       ftemp = ftemp + (oldw[i] - oldw[i+1])*(oldw[i] - oldw[i+1]);
     }
     for(i=0;i<=N_FILTER;i++)
     {
       sum = sum + oldw[i];
     }
     sum = sum/(N_FILTER+1);
     if(ftemp < noice_level && sum > 10.0 && sum < 150.)
     {
       weight = sum;
       sum = 0.0;
     }

     /*Serial.print(count);
     Serial.print("  ");
     Serial.print(noice_level);
     Serial.print("  SID=");
     Serial.print(g_sid);
     Serial.print("  ");
     Serial.print(ftemp);
     Serial.print("  ");
     Serial.println(sum);*/
     count = 0;
  }
}
//=================================================
void establishContact() 
//=================================================
{
 while (Serial.available() <= 0) 
 {
    Serial.print("X;"); 
    digitalWrite(PIN_LED_STATUS,HIGH);  delay(200);
    delay(300);
    digitalWrite(PIN_LED_STATUS,LOW);   delay(200);
  }
  //char x = Serial.read();
}
//=================================================
void setup() {
//================================================= 
    
  int i;
  
  g_led[1] = 5;
  g_led[2] = 6;
  g_led[3] = 7;  
  
  sid[0] = SIDN;
  sid[1] = SID1;
  sid[2] = SID2;
  sid[3] = SID3;
  
  Serial.begin(9600);

  pinMode(PIN_LED_STATUS, OUTPUT);

  pinMode(PIN_SIGNAL,     INPUT);
  pinMode(PIN_LED_ARM,    OUTPUT);
  pinMode(PIN_LED_MSG,    OUTPUT);
  pinMode(PIN_INTERRUPT,  OUTPUT);
  pinMode(PIN_LED_SID1,   OUTPUT);
  pinMode(PIN_LED_SID2,   OUTPUT);
  pinMode(PIN_LED_SID3,   OUTPUT);
  pinMode(PIN_BUTTON_1,   INPUT);
  pinMode(PIN_BUTTON_2,   INPUT);
  
  digitalWrite(4,HIGH);  delay(200);
  digitalWrite(5,HIGH);  delay(200);
  digitalWrite(6,HIGH);  delay(200);
  digitalWrite(7,HIGH);  delay(200);

  digitalWrite(4,LOW);   delay(200);
  digitalWrite(5,LOW);   delay(200);
  digitalWrite(6,LOW);   delay(200);
  digitalWrite(7,LOW);   delay(200);

  digitalWrite(PIN_LED_MSG,LOW);
  digitalWrite(PIN_LED_ARM,LOW);
  digitalWrite(PIN_LED_SID1,LOW);
  digitalWrite(PIN_LED_SID2,LOW);
  
  attachInterrupt(0, stamp, FALLING);
  for(i=N_FILTER;i>0;i--)oldw[i] =0.;  
  factor = refKg/(refValue-zero_level); 

  establishContact(); 
  
  setLed(1);
  cur = 1;
  g_selected_sid = selectSid();
  
  tone(PIN_INTERRUPT, 32767);

}

//=================================================
void loop()
//=================================================
{
      digitalWrite(PIN_LED_ARM,HIGH);
      if(weight > 4.0)
      {
       int s = sid[g_selected_sid];
       sendNDS(s,weight);
       //Serial.println(weight);
       digitalWrite(PIN_LED_MSG,HIGH);
       digitalWrite(PIN_LED_ARM,LOW);
       noTone(PIN_INTERRUPT);
       delay(10000);
       digitalWrite(PIN_LED_MSG,LOW);
       selectSid(); 
       weight = 0.0;
       nvalue = 0;
       tone(PIN_INTERRUPT, 32767);
     }
     
  if(digitalRead(11) == HIGH)setup();
  delay(150); 
}

